/**
 * Created by liyun on 2016/12/27.
 */

app.controller('YanZhiBaoCtrl', function($timeout, $scope, $ionicConfig,$state, $interval, $ionicSideMenuDelegate, $rootScope, $ionicHistory, $ionicPopup, $stateParams, BthSevice, PopupService, $http, $myconfig, SPAService, FetchDataService) {
    $scope.userMes = JSON.parse(localStorage.getItem('user'));




    //判定当前设备类型是ios还是android
    // var u = navigator.userAgent;
    // var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
    // var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    // alert('是否是Android：'+isAndroid);
    // alert('是否是iOS：'+isiOS);




    $scope.slideOpen = false; //是否打开侧边栏
    $scope.toggleLeft = function() {
        $ionicSideMenuDelegate.toggleLeft();
        $scope.slideOpen = true;
        var timer = $interval(function () { // 设置个定时器判断侧边栏是否打开，如果是关闭状态则隐藏黑色透明蒙层
            if(!$ionicSideMenuDelegate.isOpen()){
                $scope.slideOpen = false;
                $interval.cancel(timer);
            }
        }, 200);
    };

    $scope.goUpKeep = function (type,_time) {
        if ((SPAService.coryzaData.type && SPAService.coryzaData.type != type.type) || (SPAService.upKeepData.type && SPAService.upKeepData.type != type.type)) {
            $scope.typename = SPAService.title;
            var myPopup = $ionicPopup.show({
                templateUrl: 'templates/bitongbao/module/isSPA.html',
                scope: $scope
            });
            $scope.closeSPATip = function() {
                myPopup.close();
            }
            return;
        }

        // $rootScope.Yzb_time = _time;
        // $rootScope.connected = true;
        // $state.go('app.UpKeeping', type);
        // return;

        BthSevice.isConnect(function (status) { // 先判断程序是否已经连接上
            if($rootScope.bthshow && status){
                $rootScope.Yzb_time = _time;
                $rootScope.connected = true;
                $state.go('app.UpKeeping', type);
            }else{ // 提示未连接设备
                // PopupService.lanya($scope);
                var myPopup = $ionicPopup.show({
                    templateUrl: 'templates/bitongbao/module/lanya.html',
                    scope: $scope
                });
                $scope.myclose = function() {
                    myPopup.close();
                    $ionicHistory.nextViewOptions({
                        disableAnimate: true,
                        disableBack: true,
                        historyRoot: true
                    });
                    $state.go('app.MyLink');
                };
            }
        });
        // $state.go('app.UpKeeping', type);
    };

    //默认样式定义
    $scope.downShow = {
        hidden:false
    };

    //定义样式定时隐藏
    $scope.downHidd = function(){
            $timeout(function(){
                $scope.downShow.hidden = true;
            },5000);
    };
    $scope.downHidd();

   //设备模式强度数据请求
    /*$scope.mode_mes = function(){
        $http.get($myconfig.api.base_host + '/device/device_info?device_id=2')
        .then(function(res){
            for(var i = 0;i<res.data.data.mode.length;i++){
                res.data.data.mode[i].img_all = [];
                res.data.data.mode[i].img_half = [];
                res.data.data.mode[i].img_no = [];
                if(res.data.data.mode[i].strength = 1){
                    var a = 0,b=1,c=2;
                    res.data.data.mode[i].img_all.length = a;
                    res.data.data.mode[i].img_half.length = b;
                    res.data.data.mode[i].img_no.length = c;
                }else if(res.data.data.mode[i].strength == 1.5){
                    var a = 1,b=0,c=2;
                    res.data.data.mode[i].img_all.length = a;
                    res.data.data.mode[i].img_half.length = b;
                    res.data.data.mode[i].img_no.length = c;
                }else if(res.data.data.mode[i].strength == 2 || res.data.data.mode[i].strength == 2.5){
                    var a = 1,b=1,c=1;
                    res.data.data.mode[i].img_all.length = a;
                    res.data.data.mode[i].img_half.length = b;
                    res.data.data.mode[i].img_no.length = c;
                }else if(res.data.data.mode[i].strength == 3 || res.data.data.mode[i].strength == 3.5){
                    var a = 2,b=0,c=1;
                    res.data.data.mode[i].img_all.length = a;
                    res.data.data.mode[i].img_half.length = b;
                    res.data.data.mode[i].img_no.length = c;
                }else if(res.data.data.mode[i].strength == 4 || res.data.data.mode[i].strength == 4.5){
                    var a = 2,b=1,c=0;
                    res.data.data.mode[i].img_all.length = a;
                    res.data.data.mode[i].img_half.length = b;
                    res.data.data.mode[i].img_no.length = c;
                }else if(res.data.data.mode[i].strength == 5){
                    var a = 3,b=0,c=0;
                    res.data.data.mode[i].img_all.length = a;
                    res.data.data.mode[i].img_half.length = b;
                    res.data.data.mode[i].img_no.length = c;
                }
            }
            $scope.mode_mes_data = res.data.data;
        })
    };
    // 自执行
     $scope.mode_mes();*/

    $scope.mode_mes_data = FetchDataService.getUpKeepData(); //获取焕颜宝理疗设置

     var date_yzb =  new Date();

     //当日使用次数请求
     $scope.count_YZB = function(){
        $http.get($myconfig.api.base_host + '/physicallogs/count_b?dev_name='+'焕颜宝'+'&start_time=' + new Date(date_yzb.toLocaleDateString()) + '&user_id=' + $scope.userMes.id)
        .then(function(res){
            // console.log(res)
            $scope.count_YZB_data = parseInt(res.data.data.count);
        })
     };

     if($scope.userMes){
        $scope.count_YZB();
     }

    $scope.$on('$ionicView.enter', function(){

        $ionicConfig.views.swipeBackEnabled(false);

    });
});
